import axios from "axios";
import { message } from "ant-design-vue";

// 创建axios实例
const service = axios.create({
  baseURL: import.meta.env.VITE_API_URL, // api的base_url
  timeout: 5000, // 请求超时时间
});
// request拦截器
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    console.log(error); // for debug
    Promise.reject(error);
  }
);

// response响应拦截器
service.interceptors.response.use(
  (response) => {
    const res = response.data;
    if (res.code !== 200) {
      message.error(res.msg || "Error");
      return Promise.reject(new Error(res.msg || "Error"));
    } else {
      return res;
    }
  },
  (error) => {
    console.log("err" + error); // for debug
    message.error(error.message);
    return Promise.reject(error);
  }
);

export default service ;
